import {http} from "@/service/http";
import {Page, PageQuery} from "@/types/response";
import {MenuTree, PermissionPartial, RolesIds} from "@/types/permission";
import {MenuPartial} from "@/types/menu";

const prefix = '/Permission'


/**
 * 添加一条信息
 * @param permission 模块信息
 */
const createPermission = (permission: PermissionPartial) => {
  return http({
    method: 'POST',
    url: `${prefix}/Post`,
    data: permission
  })
}


/**
 * 更新系统菜单
 * @param permission
 */
const updatePermissionInfo = (permission: PermissionPartial) => {
  return http({
    method: 'PUT',
    url: `${prefix}/Put`,
    data: permission
  })
}

/**
 * 删除接口信息
 * @param id
 */
const removeModuleInfo = (id: string | number) => {
  return http({
    method: 'DELETE',
    url: `${prefix}/Delete/${id}`,
  })
}


/**
 * 系统菜单同步接口
 * @param action string
 * @param controllerName string
 * @param pid number
 * @param isAction boolean
 */
const getMigratePermissionPage = (action: string, controllerName: string, pid: number, isAction: boolean) => {
  return http<PermissionPartial[]>({
    method: 'GET',
    url: `${prefix}/MigratePermission`,
    params: {
      action,
      controllerName,
      pid,
      isAction,
    }
  })
}

/**
 * 查询树形table
 * @param f 父节点
 * @param key 关键词
 */
const getGetTreeTable = (f: string | number, key: string = '') => {
  return http<PermissionPartial[]>({
    method: 'GET',
    url: `${prefix}/GetTreeTable`,
    params: {
      f,
      key,
    }
  })
}

/**
 * 获取菜单树
 * @param pid id
 * @param needbtn boolean
 */
const getGetPermissionTree = (pid: number | string, needbtn: boolean) => {
  return http<MenuTree>({
    method: 'GET',
    url: `${prefix}/GetPermissionTree`,
    params: {
      pid,
      needbtn,
    }
  })
}


/**
 * 通过角色获取菜单
 * @param rid
 */
const getPermissionIdByRoleId = (rid: number | string) => {
  return http<RolesIds>({
    method: 'GET',
    url: `${prefix}/GetPermissionIdByRoleId`,
    params: {
      rid
    }
  })
}


/**
 * 获取路由树
 * @param uid
 */
const getNavigationBarPro = (uid: number | string) => {
  return http<MenuPartial[]>({
    method: 'GET',
    url: `${prefix}/GetNavigationBarPro`,
    params: {
      uid
    }
  })
}

/**
 * 通过角色获取菜单
 * @param uid
 */
const getNavigationBar = (uid: number | string) => {
  return http({
    method: 'GET',
    url: `${prefix}/GetNavigationBar`,
  })
}


/**
 * 通过角色获取菜单
 * @param page PageQuery
 */
const getMenuPage = (page: PageQuery) => {
  return http<Page<PermissionPartial[]>>({
    method: 'GET',
    url: `${prefix}/Get`,
    params: {
      ...page
    }
  })
}


/**
 * 删除菜单
 * @param id
 */
const removeMenu = (id: number | string) => {
  return http({
    method: 'DELETE',
    url: `${prefix}/Delete`,
    params: {
      id
    }
  })
}


/**
 * 导入多条菜单信息
 * @param id
 */
const createBatchPost = (permissionPartials: PermissionPartial[]) => {
  return http({
    method: 'POST',
    url: `${prefix}/BatchPost`,
    data: {
      permissionPartials
    }
  })
}


/**
 * 保存菜单权限分配
 * @param pids
 * @param rid
 */
const createAssign = (pids: number[] | string[], rid: number | string) => {
  return http({
    method: 'POST',
    url: `${prefix}/Assign`,
    data: {
      pids, rid
    }
  })
}
